import '@/common/rem.js'
import Vue from 'vue'
import VueRouter from 'vue-router'
import eventBus from '@/common/eventBus'
import { Icon, Lazyload, SwipeItem, Toast } from 'vant'
import 'vant/lib/vant-css/icon-local.css'
import './style/vant.css'
import App from './App'
import routes from './routes'

Vue.use(VueRouter)
Vue.use(Lazyload)
Vue.use(SwipeItem)
Vue.use(Toast)
Vue.use(Icon)
Vue.use(eventBus)
window.shadeVisible = null;
const userInfoStorage = window.sessionStorage.getItem('token')
let userInfo = {}
if (userInfoStorage) {
    userInfo = JSON.parse(JSON.stringify(userInfoStorage))
}
Vue.prototype.State = {
        updateInfo(info) {
            // this.userInfo.member_name = info.member_name
            // this.userInfo.avatar = info.avatar
            window.sessionStorage.setItem('token', JSON.stringify(info))
        },
        isLogin: !!userInfoStorage,
        token: userInfoStorage || '',
        userInfo
    }
    // beforeLogin.get().beforeEach((to, form, next) => {
    //     let token = sessionStorage.getItem('token')
    //     if (!token) {
    //         next('/login')
    //     }
    // })
const router = new VueRouter({ routes })
router.beforeEach((to, form, next) => {
    // const nextRoute = ['home', 'shop', 'treasure', 'usercenter'];
    let isLogin = sessionStorage.getItem('token'); // 是否登录，一般从storage中读取
    // 未登录状态；当路由到其他页，跳转至login
    if (isLogin && to.name != 'login') {
        window['shadeVisible'] = null;
        next()
    } else if (to.name === 'login') {
        if (!!to.query.error) {
            if (!window['shadeVisible']) {
                window['shadeVisible'] = Toast.error('您已登录超时，请重新登录')
            }
        }
        window.sessionStorage.removeItem('token')
        next()
    } else {
        next('/login')
    }
})
const app = new Vue({
    el: '#app',
    router,
    render: h => h(App)
})

export default app